package org.hismig.test.cfg;

import java.util.List;

import junit.framework.TestCase;

import org.hismig.cfg.Configuration;
import org.hismig.cfg.DBConnection;
import org.hismig.cfg.DBData;
import org.hismig.cfg.DataBase;
import org.hismig.cfg.DynamicPartition;
import org.hismig.cfg.ExtExecutor;
import org.hismig.cfg.ExtExecutor.Type;
import org.hismig.cfg.ParamBuilder;
import org.hismig.cfg.Strategy;
import org.hismig.cfg.Table;
import org.hismig.cfg.Trigger;
import org.hismig.utils.ListTool;

/**
 * @declare: //TODO <br />
 *           This class created by Vansion.Hopihit on 2012-07-04 <br />
 *           <a href="mailto:vansionhopihit@gmail.com">Contact Me</a>
 * @version 1.0
 * @author Vansion.Hopihit
 * 
 */
public class ConfigurationTester extends TestCase {

	Configuration configuration;

	@Override
	protected void setUp() throws Exception {
		super.setUp();

		configuration = new Configuration(ConfigurationTester.class
				.getResource("HisMig.cfg.xml").toURI());

	}

	public void testFirst() {
		assertNotNull(configuration);
	}

	public void testReadTriggerCfg() {
		Trigger trigger = configuration.getTrigger();
		assertNotNull(trigger);

		assertEquals("org.hismig.scheduler.QuartzTrigger", trigger.getClassName());
		assertEquals("TriggerExpression",
				ListTool.getParamByName(trigger.getParams(), "expression"));
	}

	public void testReadStrategyCfg() {
		Strategy strategy = configuration.getStrategy();

		assertNotNull(strategy);

		assertEquals("org.hismig.migration.strategy.RSMigration", strategy.getClassName());
		assertEquals("TRUE", strategy.getParamByName("delete_source"));
		assertEquals("SET IDENTITY_INSERT #{table} ON", strategy.getParamByName("identity_switch"));
	}

	public void testReadDatabaseCfg() {

		List<DataBase> databases = configuration.getDatabases();

		assertNotNull(databases);
		assertFalse(databases.isEmpty());

		assertEquals(2, databases.size());
	}

	public void testReadDBDataCfg() {

		DataBase db1 = ListTool.getDataBaseByName(configuration.getDatabases(),
				"Test1");

		DBData dbData = db1.getSourceDBData();
		assertEquals(db1, dbData.getParent());
		
		DynamicPartition dynamicPartition = dbData.getDynamicPartition();
		assertNull(dynamicPartition);
//		assertEquals(dbData, dynamicPartition.getParent());
		
		DBConnection dbc = dbData.getDbConnection();
		assertEquals(dbData, dbc.getParent());
		
		assertTrue(dbc.isEncrypt());
		assertEquals("net.sourceforge.jtds.jdbc.Driver",
				ListTool.getParamByName(dbc.getParams(), "driver"));
		assertEquals("jdbc:jtds:sqlserver://localhost:1433/Test1;tds=8.0;lastupdatecount=true",
				ListTool.getParamByName(dbc.getParams(), "url"));
		assertEquals("AUTEK",
				ListTool.getParamByName(dbc.getParams(), "user"));
		assertEquals("FLYVIDEO",
				ListTool.getParamByName(dbc.getParams(), "password"));

		dbc = db1.getDistDBData().getDbConnection();
		dynamicPartition = db1.getDistDBData().getDynamicPartition();
		assertEquals(db1.getDistDBData(), dynamicPartition.getParent());
		
		
		assertTrue(dynamicPartition.isEncrypt());
		assertEquals("org.hismig.partition.mssqlserver.DayPartition", dynamicPartition.getClassName());
		assertEquals("net.sourceforge.jtds.jdbc.Driver", ListTool.getParamByName(
				dynamicPartition.getParams(), "driver"));
		assertEquals("jdbc:jtds:sqlserver://localhost:1433/#{DBNAME};tds=8.0;lastupdatecount=true", ListTool.getParamByName(
				dynamicPartition.getParams(), "baseUrl"));
		assertEquals("jdbc:jtds:sqlserver://localhost:1433/master;tds=8.0;lastupdatecount=true",
				ListTool.getParamByName(dynamicPartition.getParams(), "createDBUrl"));
		assertEquals("AUTEK",
				ListTool.getParamByName(dynamicPartition.getParams(), "user"));
		assertEquals("FLYVIDEO",
				ListTool.getParamByName(dynamicPartition.getParams(), "password"));
		assertEquals("30",
				ListTool.getParamByName(dynamicPartition.getParams(), "scope"));
		assertEquals("D:/",
				ListTool.getParamByName(dynamicPartition.getParams(), "db_path"));
		assertEquals("5MB",
				ListTool.getParamByName(dynamicPartition.getParams(), "dat_size"));
		assertEquals("10MB",
				ListTool.getParamByName(dynamicPartition.getParams(), "dat_maxsize"));
		assertEquals("5",
				ListTool.getParamByName(dynamicPartition.getParams(), "dat_filegrowth"));
		
	}

	public void testReadExtExecutorCfg() {
		DataBase db1 = ListTool.getDataBaseByName(configuration.getDatabases(),
				"Test1");

		ExtExecutor extExecutor = db1.getSourcePreExtExecutor();
		assertEquals(db1, extExecutor.getParent());

		assertEquals("org.hismig.migration.executor.ext.SqlExecutor", extExecutor.getClassName());
		assertEquals("source", extExecutor.getTo());
		assertEquals(Type.PRE, extExecutor.getType());
		assertEquals("if exists (select 1 from sysobjects where name='TESTER') begin drop table TESTER end create table TESTER(autoid int identity, num int)",
				ListTool.getParamByName(extExecutor.getParams(), "sql"));

		extExecutor = db1.getDistPreExtExecutor();
		assertEquals(db1, extExecutor.getParent());

		assertEquals("org.hismig.migration.executor.ext.SqlExecutor", extExecutor.getClassName());
		assertEquals("dist", extExecutor.getTo());
		assertEquals(Type.PRE, extExecutor.getType());
		assertEquals("if exists (select 1 from sysobjects where name='TESTER1') begin drop table TESTER1 end",
				ListTool.getParamByName(extExecutor.getParams(), "sql"));

		extExecutor = db1.getSourceAfterExtExecutor();
		assertEquals(db1, extExecutor.getParent());
		assertEquals("org.hismig.migration.executor.ext.SqlExecutor", extExecutor.getClassName());
		assertEquals("source", extExecutor.getTo());
		assertEquals(Type.AFTER, extExecutor.getType());
		assertEquals("update TESTER set num=num+1",
				ListTool.getParamByName(extExecutor.getParams(), "sql"));

		extExecutor = db1.getDistAfterExtExecutor();
		assertEquals(db1, extExecutor.getParent());
		assertEquals("org.hismig.migration.executor.ext.SqlExecutor", extExecutor.getClassName());
		assertEquals("dist", extExecutor.getTo());
		assertEquals(Type.AFTER, extExecutor.getType());
		assertEquals("update TESTER set num=num-1",
				ListTool.getParamByName(extExecutor.getParams(), "sql"));
	}

	public void testReadParamBuilderCfg() {
		DataBase db1 = ListTool.getDataBaseByName(configuration.getDatabases(),
				"Test1");

		List<ParamBuilder> paramBuilders = db1.getParamBuilders();

		assertEquals(3, paramBuilders.size());

		ParamBuilder pb = paramBuilders.get(0);
		assertEquals(db1, pb.getParent());

		assertEquals("org.hismig.migration.parambuilder.NetureDayBuilder", pb.getClassName());
		assertEquals("day1", pb.getForName());
		assertEquals("yyyyMMdd", ListTool.getParamByName(pb.getParams(), "pattern"));

		pb = paramBuilders.get(1);
		assertEquals(db1, pb.getParent());
		
		assertEquals("org.hismig.migration.parambuilder.NetureDayBuilder", pb.getClassName());
		assertEquals("day2", pb.getForName());
		assertEquals("yyyyMMdd", ListTool.getParamByName(pb.getParams(), "pattern"));
		assertEquals("-1", ListTool.getParamByName(pb.getParams(), "scope"));
		
		pb = paramBuilders.get(2);
		assertEquals(db1, pb.getParent());
		
		assertEquals("org.hismig.migration.parambuilder.NetureDayBuilder", pb.getClassName());
		assertEquals("day3", pb.getForName());
		assertEquals("yyyyMMdd", ListTool.getParamByName(pb.getParams(), "pattern"));
		assertEquals("1", ListTool.getParamByName(pb.getParams(), "scope"));
	}

	public void testReadTableCfg() {
		DataBase db1 = ListTool.getDataBaseByName(configuration.getDatabases(),
				"Test1");

		assertEquals(2, db1.getTables().size());

		Table table = ListTool.getTableByName(db1.getTables(), "TESTER");
		assertEquals(db1, table.getParent());

		assertEquals("org.hismig.migration.executor.ext.SqlExecutor", table.getSourcePreExtExecutor()
				.getClassName());

		assertEquals("org.hismig.migration.executor.ext.SqlExecutor", table.getDistPreExtExecutor()
				.getClassName());
		assertEquals("update TESTER set num=num-200", ListTool.getParamByName(table
				.getDistPreExtExecutor().getParams(), "sql"));

		assertEquals("org.hismig.migration.executor.ext.SqlExecutor", table
				.getSourceAfterExtExecutor().getClassName());
		assertEquals("update TESTER set num=num+10", ListTool.getParamByName(table
				.getSourceAfterExtExecutor().getParams(), "sql"));

		assertEquals("org.hismig.migration.executor.ext.SqlExecutor", table
				.getDistAfterExtExecutor().getClassName());
		assertEquals("update TESTER set num=num+1", ListTool.getParamByName(table
				.getDistAfterExtExecutor().getParams(), "sql"));

		assertEquals("org.hismig.migration.parambuilder.NetureDayBuilder", table.getParamBuilders().get(0)
				.getClassName());
		assertEquals("currentdate", table.getParamBuilders().get(0).getForName());
		assertTrue(table.getParamBuilders().get(0).getParams().isEmpty());

		assertEquals("org.hismig.migration.parambuilder.NetureDayBuilder", table.getParamBuilders().get(1)
				.getClassName());
		assertEquals("workdate", table.getParamBuilders().get(1).getForName());
		assertEquals(
				"-5",
				ListTool.getParamByName(table.getParamBuilders().get(1)
						.getParams(), "Range"));
		assertEquals(
				"native",
				ListTool.getParamByName(table.getParamBuilders().get(1)
						.getParams(), "Type"));

		assertEquals("where num >= 1 and num <= 100", table
				.getSelectCondition().getValue());
	}

}
